//
//  TimeQuartzView.m
//  AppFinance
//
//  Created by wei on 12-6-10.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "AFVolumeQuartzView.h"
#import "AFUtils.h"
#import "AFMarketData.h"

#import "AFSetting.h"

@implementation AFVolumeQuartzView

- (void) distory {
    isDistroy = YES;
    
    [self setNeedsDisplay];
}

-(void)drawInContext:(CGContextRef)context rect:(CGRect)rect{
    
    NSLog(@" -------------------- 刷新 分时图 ------------------------");

    if (isDistroy) {
        isDistroy = NO;
        [self draw4distory:context];
        return;
    }
    
    // 绘制背景框
    [self drawBackGround:context];

    // 开始 绘制 交易量
    [self drawVolumeLine:context];
}

- (void) draw4distory:(CGContextRef)context{
    // 绘制前，先清楚所有绘图区域
    CGContextClearRect(context, CGRectMake(0, 0, [AFSetting QUARTZ_VOLUME_W], [AFSetting QUARTZ_VOLUME_H]));
    
    // 绘制背景框
    [self drawBackGround:context];
}

// 绘制分时图背景框
-(void) drawBackGround :(CGContextRef)context{
    
    // 四周得矩形框
	CGFloat bg_ling_width = 1.0f;
    
    // 整个绘图区域大小 320 * 254
    // 绘制前，先清楚所有绘图区域
    CGContextClearRect(context, CGRectMake(0, 0, [AFSetting QUARTZ_VOLUME_W], [AFSetting QUARTZ_VOLUME_H]));
    
    // 设置线的颜色，红色
    //CGContextSetRGBStrokeColor(context, 1.0, 0.0, 0.0, 1.0);
    CGContextSetStrokeColorWithColor(context, AF_K_QUARTZ_DRAW_BORDER_CGCOLOR);
    //CGContextSetLineCap(context, cap);
    //CGContextSetLineJoin(context, join);
    
    // 设置线的宽度
    CGContextSetLineWidth(context, bg_ling_width);
    
    // [S]整个矩形框范围
    CGContextAddRect(context, CGRectMake(0, 0, [AFSetting QUARTZ_VOLUME_W], [AFSetting QUARTZ_VOLUME_H])); // 分时背景框
    CGContextStrokePath(context);
    /*
    // 分割线绘制
    // --分时
    CGContextMoveToPoint(context, 1, QUARTZ_TIME_H/2);
    CGContextAddLineToPoint(context, QUARTZ_TIME_W - 2, QUARTZ_TIME_H/2);
    CGContextMoveToPoint(context, QUARTZ_TIME_W/2, 1);
    CGContextAddLineToPoint(context, QUARTZ_TIME_W/2, QUARTZ_TIME_H - 2);
    CGContextStrokePath(context);
    */
    
    // 分时背景虚线
    // 虚线设置
    CGContextSetStrokeColorWithColor(context, AF_K_QUARTZ_DRAW_BORDER_CGCOLOR_daush);
    CGContextSetLineWidth(context, 0.2);
    
   	CGFloat dashPhase = 2.0;
	CGFloat dashPattern[2] = {4.0, 4.0};
	size_t dashCount = 2;
    CGContextSetLineDash(context, dashPhase, dashPattern, dashCount);
    
    // 竖线
    CGContextMoveToPoint(context, [AFSetting QUARTZ_VOLUME_W]/4, 1);
    CGContextAddLineToPoint(context, [AFSetting QUARTZ_VOLUME_W]/4, [AFSetting QUARTZ_VOLUME_H] - 2);
    
    CGContextMoveToPoint(context, [AFSetting QUARTZ_VOLUME_W]/2, 1);
    CGContextAddLineToPoint(context, [AFSetting QUARTZ_VOLUME_W]/2, [AFSetting QUARTZ_VOLUME_H] - 2);
    
    CGContextMoveToPoint(context, [AFSetting QUARTZ_VOLUME_W]/4*3, 1);
    CGContextAddLineToPoint(context, [AFSetting QUARTZ_VOLUME_W]/4*3, [AFSetting QUARTZ_VOLUME_H] - 2);
    
    
    // 横线
    CGContextMoveToPoint(context, 1, [AFSetting QUARTZ_VOLUME_H]/2);
    CGContextAddLineToPoint(context, [AFSetting QUARTZ_VOLUME_W], [AFSetting QUARTZ_VOLUME_H]/2);

    CGContextStrokePath(context);
    CGContextSetLineDash(context, 0, 0, 0); //取消虚线
}


- (void) drawVolumeLine:(CGContextRef)context{
    // [S] 交易量数据
    NSArray *localVolumeListArray = [[AFMarketData data] drawTimeVolumeListArray];
    if (localVolumeListArray== nil) {
        return;
    }
    float x1,y1,x2,y2;
    NSString *status = @"";
    
    NSDictionary *localItem = nil;
    
    CGContextSetLineWidth(context, 0.8);
    for(int k = 0; k < [localVolumeListArray count]; k++){
        localItem = [localVolumeListArray objectAtIndex:k];
        
        //status: up/down
        status = [NSString stringWithFormat:@"%@",[localItem objectForKey:@"status"]];
        x1 = [[NSString stringWithFormat:@"%@",[localItem objectForKey:@"x1"]] floatValue];
        y1 = [[NSString stringWithFormat:@"%@",[localItem objectForKey:@"y1"]] floatValue];
        x2 = [[NSString stringWithFormat:@"%@",[localItem objectForKey:@"x2"]] floatValue];
        y2 = [[NSString stringWithFormat:@"%@",[localItem objectForKey:@"y2"]] floatValue];
        
        //NSLog(@" x1 = %f, y1 = %f, x2 = %f, y2 = %f", x1, y1, x2, y2);
        
        /*
        if ([status isEqualToString:@"up"]) {
            //CGContextSetStrokeColorWithColor(context, [upColor CGColor]);
            CGContextSetRGBStrokeColor(context, 1.0, 0.0, 0.0, 1.0);
        } else {
            //CGContextSetStrokeColorWithColor(context, [downColor CGColor]);
            CGContextSetRGBStrokeColor(context, 0.0, 1.0, 0.0, 1.0);
        }
        */
        CGContextSetRGBStrokeColor(context, 250.0f/255.0f, 149.0f/255.0f, 39.0f/255.0f, 1.0);
        
        CGContextMoveToPoint(context, x1, y1);
        CGContextAddLineToPoint(context, x2, y2);
        CGContextStrokePath(context);
    }
    
    // [E] 交易量数据
}

@end
